package com.ym.controller.management;

import cn.hutool.core.date.DateUtil;
import com.ym.common.annotations.CustomConstraint;
import com.ym.common.constant.Constants;
import com.ym.common.enums.UserUserTypeEnum;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;

import com.ym.core.utils.DateKit;
import com.ym.medical.domain.vo.IndexCountUserVo;
import com.ym.medical.domain.vo.OrderConsultantReportVo;
import com.ym.medical.domain.vo.UserReportVo;
import com.ym.medical.enums.InquiryOrderIoTypeEnum;
import com.ym.medical.service.IReportCountSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;

@Api(tags = "首页报表数据")
@RestController
@RequestMapping("/management/index")
@Validated
public class IndexController extends BaseController {

	@Autowired
	private IReportCountSV reportCountSV;


	@ApiOperation("统计数据图表")
	@GetMapping(value = "/report/statistics")
	public APIResponse<IndexCountUserVo> reportCountUsers(
			@RequestParam @Valid @Pattern(regexp = "\\d{4}-\\d{2}", message = "日期格式为:yyyy-MM") String date
	) {
		Date date1 = DateUtil.parse(date, Constants.DatePattern.yyyyMM);
		String startTime = DateUtil.beginOfMonth(date1).toString();
		String endTime =  DateUtil.endOfMonth(date1).toString();
		IndexCountUserVo personnelStatistics = reportCountSV.getPersonnelStatistics(startTime, endTime);
		return successData(personnelStatistics);
	}


	@ApiOperation("咨询图表")
	@GetMapping(value = "/report/consultingStatistics")
	public APIResponse<List<OrderConsultantReportVo>> consultingStatistics(
			@RequestParam(required = false) @CustomConstraint(InquiryOrderIoTypeEnum.class) String ioType,
			@RequestParam @Valid @Pattern(regexp = "\\d{4}-\\d{2}", message = "日期格式为:yyyy-MM") String date
	) {
		Date date1 = DateUtil.parse(date, Constants.DatePattern.yyyyMM);
		String startTime = DateUtil.beginOfMonth(date1).toString();
		String endTime =  DateUtil.endOfMonth(date1).toString();
		List<OrderConsultantReportVo> data = reportCountSV.getNumberOfConsultants(startTime, endTime, ioType);
		return successData(data);
	}


	@ApiOperation("注册用户图表")
	@GetMapping(value = "/report/registeredUser")
	public APIResponse<List<UserReportVo>> registeredUser(
			@RequestParam(required = false) @CustomConstraint(UserUserTypeEnum.class) String userType,
			@RequestParam @Valid @Pattern(regexp = "\\d{4}-\\d{2}", message = "日期格式为:yyyy-MM") String date
	) {
		Date date1 = DateUtil.parse(date, Constants.DatePattern.yyyyMM);
		String startTime = DateUtil.beginOfMonth(date1).toString();
		String endTime =  DateUtil.endOfMonth(date1).toString();
		List<UserReportVo> data = reportCountSV.getNumberOfRegisteredUser(startTime, endTime, userType);
		return successData(data);
	}

}
